package com.ray.controller;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.google.appengine.repackaged.com.google.gson.JsonObject;
import com.google.appengine.repackaged.com.google.protobuf.ServiceException;
import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.FileManagerBean;
import com.ray.bean.WorkOrderBean;
import com.ray.bean.WorkOrderMachineBean;
import com.ray.bean.WorkOrderMaterialPriceDetail;
import com.ray.pojo.MyUser;
import com.ray.service.WeiXinService;
import com.ray.service.WorkOrderService;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.UploadFilesUtil;
import com.ray.util.WeiXinParamesUtil;
import com.ray.util.WeiXinUtil;
/**
 * 维修工单
 * @author Shensg
 * 2018年8月10日
 */
@Controller
@RequestMapping("workOrder")
public class WorkOrderController {
	
	@Autowired
	WorkOrderService workOrderService;
	@Autowired
	WeiXinService weiXinService;
	//图片上传
	@RequestMapping("/uploadPic")
	public Map queryFile(@RequestParam("file") CommonsMultipartFile files[],HttpServletRequest request) throws IOException, ServiceException  {  
		Map map=RequestUtil.getRequestAllParam(request);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		//return ResponseUtil.ResponseResult(UploadFilesUtil.loadFileUtils(files,request,"/uploadpic/orderpic"+sdf.format(new Date())));
		return ResponseUtil.ResponseResult(UploadFilesUtil.loadFileByQiniu4BucketShinehaopic(files, request));
	} 
	//文件上传
	@RequestMapping("/uploadFileByBase64")
	public Map uploadFileByBase64(HttpServletRequest request) throws IOException, ServiceException  {  
		Map map=RequestUtil.getRequestAllParam(request);
		List list = UploadFilesUtil.loadBase64FileByQiniu4BucketShinehaopic(map.get("base64content").toString(), "",request);
		return ResponseUtil.ResponseResult(list);
	} 
	/**
	 * 设备维修配置列表
	 */
	@RequestMapping("/getConfigList")
	@ResponseBody
	public DatatablesViewPageBean getConfigList(HttpServletRequest request,WorkOrderMachineBean bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		return workOrderService.getConfigList(map,bean);
	}
	/**
	 * 设备维修配置信息通过id
	 */
	@RequestMapping("/getConfigById")
	@ResponseBody
	public WorkOrderMachineBean getConfigById(HttpServletRequest request,WorkOrderMachineBean bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		return workOrderService.getConfigById(map,bean);
	}
	/**
	 * 获取该公司用户
	 */
	@RequestMapping("/getCompanyUserTree")
	@ResponseBody
	public Map getCompanyUserTree(HttpServletRequest request,WorkOrderMachineBean bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		List<Map> list = workOrderService.getCompanyUserTree(map);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("rows", list);
		jsonObject.put("total", list.size());
		return ResponseUtil.ResponseResult(jsonObject);
	}
	/**
	 * 获取该公司用户
	 */
	@RequestMapping("/getOrderTree")
	@ResponseBody
	public Map getOrderTree(HttpServletRequest request,WorkOrderMachineBean bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		List<Map> list = workOrderService.getOrderTree(map);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("rows", list);
		jsonObject.put("total", list.size());
		return ResponseUtil.ResponseResult(jsonObject);
	}
	/**
	 * 设备维修配置修改
	 */
	@RequestMapping("/updateConfig")
	@ResponseBody
	public Map updateConfig(HttpServletRequest request,WorkOrderMachineBean bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workOrderService.updateConfig(map,bean));
	}
	
	/**
	 * 设备维修配置增加
	 */
	@RequestMapping("/addConfig")
	@ResponseBody
	public Map addConfig(HttpServletRequest request,WorkOrderMachineBean bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workOrderService.addConfig(map,bean));
	}
	
	/**
	 * 设备二维码生成 暂时前端生成
	 */
	/**
	 * 扫码出现详情信息
	 */
	@RequestMapping("/sqr")
	public String  sh(String id,HttpServletRequest request) throws UnsupportedEncodingException {
		return "redirect:"+WeiXinParamesUtil.YUMIN+"/weixin/wxauth.html?appid="+WeiXinParamesUtil.weiXinPbulicNumberAppId+"&scope=snsapi_base&state="+id+"&redirect_uri=https://www.shinehao.net/weixin/workOrder/sqrInfo";
	}
	@RequestMapping("/sqrInfo")
	public String  sqrInfo(String id,String code,String state,Model model,HttpServletRequest request) throws com.ray.exception.ServiceException, IOException {
		id=state;
		MyUser myUser=weiXinService.getWeiXinPublicNumberReportUser(code,request,null,"/phoneApp/machineRepair#id="+id);
		if(id!=null && !"".equals(id)) {
			String accessToken=weiXinService.getAccessToken(null);
			boolean subscribe = WeiXinUtil.hasSubscribe(accessToken, myUser.getWeiXinPublicOpenId());
			if(!subscribe || null==myUser.getWeiXinPublicOpenId()||"".equals(myUser.getWeiXinPublicOpenId())) {
				return "redirect:/WeChatQrcode.html";//先去关注
			}else {
				if(null==myUser.getUserName()||"".equals(myUser.getUserName())) {
					WorkOrderMachineBean bean = new WorkOrderMachineBean();
					bean.setId(Integer.valueOf(id));
					String buyer = workOrderService.getConfigById(null,bean).getBuyer();
					myUser.setCompany(buyer);
					myUser.setUserName("无账号用户");
					myUser.setPhone("");
					myUser.setDeptId("0");
					myUser.setUserId("-1");
					request.getSession().setAttribute("userLogin", myUser);
				}
				return "redirect:/phoneApp/machineRepair#id="+id;
			}
		}else {
				model.addAttribute("weiXinPublicOpenId", myUser.getWeiXinPublicOpenId());
				model.addAttribute("weiXinPublicunionId",myUser.getUnionId());
				return "redirect:/login.jsp";
		}
	}
	/**
	 * 设备工单申请 发送模版消息到主管
	 * @throws ServiceException 
	 * machineId 二维码上的id
	 */
	@RequestMapping("/addApplyWorkOrder")
	@ResponseBody
	public Map addApplyWorkOrder(HttpServletRequest request,WorkOrderBean bean) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		workOrderService.addApplyWorkOrder(map,bean);
		return ResponseUtil.ResponseResult(bean);
	}
	
	/**
	 * 设备工单详情显示[1、申请信息 2、指派信息 3、维修信息 4、评价信息]通过配置id 找到最近一次进行中的工单
	 * 传入machineId 就是 url上id
	 */
	@RequestMapping("/getApplyWorkOrderByMachineId")
	@ResponseBody
	public Map getApplyWorkOrderByMachineId(HttpServletRequest request,WorkOrderBean bean) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		workOrderService.updateWorkOrderForOpinion();
		WorkOrderBean workOrderBean = workOrderService.getApplyWorkOrderByMachineId(map,bean);
		/*if(workOrderBean.getId()>0) {
			workOrderBean = workOrderService.getApplyWorkOrderById(map,bean);
		}*/
		return ResponseUtil.ResponseResult(workOrderBean);
	}
	/**
	 * 设备工单详情显示[1、申请信息 2、指派信息 3、维修信息 4、评价信息]通过申请id
	 */
	@RequestMapping("/getApplyWorkOrderById")
	@ResponseBody
	public Map getApplyWorkOrderById(HttpServletRequest request,WorkOrderBean bean) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		workOrderService.updateWorkOrderForOpinion();
		WorkOrderBean workOrderBean = workOrderService.getApplyWorkOrderById(map,bean);
		return ResponseUtil.ResponseResult(workOrderBean);
	}
	/**
	 * 主管确认指派人 发送模版消息通知(告知接单人和申请人)
	 */
	@RequestMapping("/updateWorkOrderWorkName")
	@ResponseBody
	public Map updateWorkOrderWorkName(HttpServletRequest request,WorkOrderBean bean) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		int total = workOrderService.updateWorkOrderWorkName(map,bean);
		return ResponseUtil.ResponseResult("指派维修师傅成功");
	}
	/**
	 * 主管确认指派人 发送模版消息通知(告知接单人和申请人)
	 */
	@RequestMapping("/updateWorkOrderWorkNameTwice")
	@ResponseBody
	public Map updateWorkOrderWorkNameTwice(HttpServletRequest request,WorkOrderBean bean) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		int total = workOrderService.updateWorkOrderWorkNameTwice(map,bean);
		return ResponseUtil.ResponseResult("指派维修师傅成功");
	}
	/**
	 * 工单详情 接单人信息填写 完成维修  发送给工单申请人维修结果
	 */
	@RequestMapping("/updateWorkOrderWorkRepaireInfo")
	@ResponseBody
	public Map updateWorkOrderWorkRepaireInfo(HttpServletRequest request,WorkOrderBean bean) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		int total = workOrderService.updateWorkOrderWorkRepaireInfo(map,bean);
		return ResponseUtil.ResponseResult("信息更新成功");
	}
	/**
	 * 工单维修服务评价 发送模版消息提醒维修技师
	 */
	@RequestMapping("/updateWorkOrderCustEvaluation")
	@ResponseBody
	public Map updateWorkOrderCustEvaluation(HttpServletRequest request,WorkOrderBean bean) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		int total = workOrderService.updateWorkOrderCustEvaluation(map,bean);
		return ResponseUtil.ResponseResult("评价成功");
	}
	/**
	 * 更新申请订单状态
	 */
	@RequestMapping("/updateApplyOrderStatus")
	@ResponseBody
	public Map updateApplyOrderStatus(HttpServletRequest request,WorkOrderBean bean) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		int total = workOrderService.updateApplyOrderStatus(map,bean);
		return ResponseUtil.ResponseResult("工单状态修改成功");
	}
	/**
	 * 更新申请订单补充说明
	 */
	@RequestMapping("/updateApplyOrderRemark")
	@ResponseBody
	public Map updateApplyOrderRemark(HttpServletRequest request,WorkOrderBean bean) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		int total = workOrderService.updateApplyOrderRemark(map,bean);
		return ResponseUtil.ResponseResult("工单状态修改成功");
	}
	/**
	 * 申请工单列表
	 */
	@RequestMapping("/getApplyList")
	@ResponseBody
	public DatatablesViewPageBean getApplyList(HttpServletRequest request,WorkOrderMachineBean bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		MyUser myuser=RequestUtil.getLoginUser(request);
		
		if(JSONObject.toJSON(myuser.getPermissionList()).toString().indexOf("superManager")>-1) {
			map.put("superManager", "superManager");
		}
		return workOrderService.getApplyList(map);
	}
	
	/**
	 * 获取物料维修报价列表
	 */
	@RequestMapping("/getMaterialPriceList")
	@ResponseBody
	public DatatablesViewPageBean getMaterialPriceList(HttpServletRequest request,WorkOrderMachineBean bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		return workOrderService.getMaterialPriceList(map);
	}
	/**
	 * 更新物料维修报价
	 */
	@RequestMapping("/updateMaterialPrice")
	@ResponseBody
	public Map updateMaterialPrice(HttpServletRequest request,WorkOrderMachineBean bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		workOrderService.updateMaterialPrice(map);
		return ResponseUtil.ResponseResult("维修报价修改成功");
	}
	/**
	 * 通过关联订单编号获取物料价目
	 * @param request
	 * @param bean
	 * @return
	 */
	@RequestMapping("/getMaterialPriceByOrderNo")
	@ResponseBody
	public Map getMaterialPriceByOrderNo(HttpServletRequest request,WorkOrderMachineBean bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workOrderService.getMaterialPriceByOrderNo(map));
	}
	/**
	 * 维修报价增加
	 * @param request
	 * @param bean
	 * @return
	 */
	@RequestMapping("/addWorkRepairPrice")
	@ResponseBody
	public Map addWorkRepairPrice(HttpServletRequest request,WorkOrderMaterialPriceDetail bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workOrderService.addWorkRepairPrice(map,bean));
	}
	/**
	 * 维修报价删除
	 * @param request
	 * @param bean
	 * @return
	 */
	@RequestMapping("/updateWorkRepairPriceById")
	@ResponseBody
	public Map updateWorkRepairPriceById(HttpServletRequest request,WorkOrderMaterialPriceDetail bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		workOrderService.updateWorkRepairPriceById(bean);
		return ResponseUtil.ResponseResult("删除成功");
	}
	/**
	 * 维修报价删除
	 * @param request
	 * @param bean
	 * @return
	 */
	@RequestMapping("/getWorkRepairPriceList")
	@ResponseBody
	public Map getWorkRepairPriceList(HttpServletRequest request,WorkOrderMaterialPriceDetail bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workOrderService.getWorkRepairPriceList(map,bean));
	}
	/**
	 * 维修报价结算价格
	 * @param request
	 * @param bean
	 * @return
	 */
	@RequestMapping("/updateBalancePrice")
	@ResponseBody
	public Map updateBalancePrice(HttpServletRequest request,String id,String balancePrice) {
		Map map=RequestUtil.getRequestAllParam(request);
		workOrderService.updateBalancePrice(id,balancePrice);
		return ResponseUtil.ResponseResult("保存成功");
	}
	/**
	 * 获取设备维修汇总看板所需数据
	 * @author nate 
	 * @param request
	 * @return
	 */
	@RequestMapping("/getWorkRepairSummary")
	@ResponseBody
	public Map getWorkRepairSummary(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workOrderService.getWorkRepairSummary(map));
	}
	
	
	/**
	 * 获取设备警报汇总看板所需数据
	 * @author nate 
	 * @param request
	 * @return
	 */
	@RequestMapping("/getWorkAlarmSummary")
	@ResponseBody
	public Map getWorkAlarmSummary(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workOrderService.getWorkAlarmSummary(map));
	}
	
	/**
	 * 获取设备警报类型
	 * @param request
	 * @return
	 */
	@RequestMapping("/getEquimentAlarmTypeName")
	@ResponseBody
	public Map getEquimentAlarmTypeName(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workOrderService.getEquimentAlarmTypeName(map));
	}
	
	/**
	 * 批量修改设备维修主管和技工
	 */
	@RequestMapping("/updateWorkMachineManagerAndWorksBatch")
	@ResponseBody
	public Map updateWorkMachineManagerAndWorksBatch(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		workOrderService.updateWorkMachineManagerAndWorksBatch(map);
		return ResponseUtil.ResponseResult("修改成功");
	}
	
	/**
	 * 获取每个月维修数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/getWorkOrderApplyDataEveryMonth")
	@ResponseBody
	public Map getWorkOrderApplyDataEveryMonth(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workOrderService.getWorkOrderApplyDataEveryMonth(map));
	}
	
	/**
	 * 获取distinct所有数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/getDistrict")
	@ResponseBody
	public Map getDistrict(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workOrderService.getDistrict(map));
	}
	
	/**
	 * 更新修改 等级薪资基础表
	 * @param request
	 * @return
	 */
	@RequestMapping("/insertOrUpdateWorkOrderSalaryType")
	@ResponseBody
	public Map insertOrUpdateWorkOrderSalaryType(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		int res = workOrderService.insertOrUpdateWorkOrderSalaryType(map);
		if(res==-1) {
			return ResponseUtil.ResponseError("编辑未成功,请检查维修评价类型是否有重复");
		}
		return ResponseUtil.ResponseResult("数据编辑成功");
	}
	
	/**
	 * 更新修改 星级评价表
	 * @param request
	 * @return
	 */
	@RequestMapping("/insertOrUpdateWorkOrderStarType")
	@ResponseBody
	public Map insertOrUpdateWorkOrderStarType(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		int res = workOrderService.insertOrUpdateWorkOrderStarType(map);
		if(res==-1) {
			return ResponseUtil.ResponseError("编辑未成功,请检查星级类型是否有重复");
		}
		return ResponseUtil.ResponseResult("数据编辑成功");
	}
	
	/**
	 * 更新修改 回访评价表
	 * @param request
	 * @return
	 */
	@RequestMapping("/insertOrUpdateWorkOrderVisitType")
	@ResponseBody
	public Map insertOrUpdateWorkOrderVisitType(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		int res = workOrderService.insertOrUpdateWorkOrderVisitType(map);
		if(res==-1) {
			return ResponseUtil.ResponseError("编辑未成功,请检查回访类型是否有重复");
		}
		return ResponseUtil.ResponseResult("数据编辑成功");
	}
	
	/**
	 * 获取基本基础表 回访 星级评价列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getBasicInfoForSalaryList")
	@ResponseBody
	public Map getBasicInfoForSalaryList(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		List<Map> retMap = workOrderService.getBasicInfoForSalaryList(map);
		int total = workOrderService.getBasicInfoForSalaryCount(map);
		
		DatatablesViewPageBean bean = new DatatablesViewPageBean();
		bean.setReturnData(retMap);
		bean.setiTotalDisplayRecords(total);
		bean.setiTotalRecords(total);
		return ResponseUtil.ResponseResult(bean);
	}
	
	/**
	 * 获取基本基础表 回访 星级评价
	 * @param request
	 * @return
	 */
	@RequestMapping("/getBasicInfoForSalary")
	@ResponseBody
	public Map getBasicInfoForSalary(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workOrderService.getBasicInfoForSalary(map));
	}
	
	
	/**
	 * 更新回访
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateWorkOrderApplyForVisit")
	@ResponseBody
	public Map updateWorkOrderApplyForVisit(HttpServletRequest request,WorkOrderBean bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		workOrderService.updateWorkOrderApplyForVisit(map,bean);
		return ResponseUtil.ResponseResult("工单回访成功");
	}
	/**
	 * 计算维修订单薪资
	 * @param request
	 * @param bean
	 * @return
	 */
	@RequestMapping("/updateWorkOrderApplyForSalary")
	@ResponseBody
	public Map updateWorkOrderApplyForSalary(HttpServletRequest request,WorkOrderBean bean) {
		Map map=RequestUtil.getRequestAllParam(request);
		workOrderService.updateWorkOrderApplyForSalary(map,bean);
		return ResponseUtil.ResponseResult("工单评级成功");
	}
	
	/**
	 * 获取在职维修人员列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getServiceWorkerList")
	@ResponseBody
	public Map getServiceWorkerList(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workOrderService.getServiceWorkerList(map));
	}
	
	/**
	 * 保存到新的维修操作表中
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveWorkOrderInfoForCustmerOperate")
	@ResponseBody
	public Map saveWorkOrderInfoForCustmerOperate(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		workOrderService.saveWorkOrderInfoForCustmerOperate(map);
		return ResponseUtil.ResponseResult("保存成功");
	}
	
	@RequestMapping("/getWorkOrderInfoForCustmerOperate")
	@ResponseBody
	public Map getWorkOrderInfoForCustmerOperate(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workOrderService.getWorkOrderInfoForCustmerOperate(map));
	}
}

